<?php
class Merchant_Service_ProductStocket
{
	public function __construct(){
		
	}
	
	public static function getByProductId($id,$warehouse_id=1){		
		$obj = new Merchant_Model_ProductStocket();
		return $obj->getByProductId($id,$warehouse_id);		
	}
	
	public static function getByProductIdCustomerId($productId,$customerId=null){
		$wps = self::getByCondition(array("product_id"=> $productId));
		if(is_array($wps) && !empty($wps)){
			$productService = new Merchant_Service_Product();
			foreach($wps as $key=>$value){
				$product = $productService->getProductInfo($value['product_id'],$customerId);
				if(is_array($product) && !empty($product)){
					$wps[$key]['product'] = $product;
				}else{
					unset($wps[$key]);
				}
			}
				
			return empty($wps)?false:$wps;
		}
		return false;
	}
	
	

	public static function getByCondition($condition=array(),$type='*',$order=array('product_id'), $pageSize=0, $page=1)
	{ 
		$obj=new Merchant_Model_ProductStocket();
		$rows= $obj->getByCondition( $condition,$type,$order, $pageSize,$page);	
		return $rows;
	}
	
	public static function getByConditionJoin($condition=array(),$type='*',$order=array('product_id'), $page=1,$pageSize=0)
	{
		$obj=new Merchant_Model_ProductStocket();
		$rows= $obj->getByConditionJoin( $condition,$type,$order, $page,$pageSize);
		return $rows;
	}
	
	public function getAdapter(){
		$model = new Merchant_Model_ProductStocket();
		return $model->getAdapter();
	}
	
	public function update($row,$id,$warehouse_id){
		$model = new Merchant_Model_ProductStocket();
		return $model->update($row,$id,$warehouse_id);
	}
	
	public static function create($row)
	{ 
		$obj=new Merchant_Model_ProductStocket();
		return $obj->add($row);
	}
	
	
	
	/*
	*
	*产品下架
	*
	*/
	public static function pickingProcdut($productId,$quantity,$warehouseId,$customerId=null,$remark=""){
		$obj = new Merchant_Model_ProductStocket();
		$rowProduct = $obj->getByProductId($productId,$warehouseId);

		if($quantity <= $rowProduct['ps_quantity']){
			$currentStock = $rowProduct['ps_quantity']-$quantity;
			$currentHold = $rowProduct['ps_quantity_hold']+$quantity;
			$timeNow = date('Y-m-d H:i:s');
			$whProduct =	array('ps_quantity'=>$currentStock ,
								'ps_quantity_hold'=>$currentHold,
								'ps_last_update'=>$timeNow);
							
			$result= $obj->update($whProduct,$productId,$warehouseId);
			
			
			if(!$customerId){
			
				$session = new Zend_Session_Namespace('customer');
				$customerId = $session->customer['customer_id'];	
			}
							//操作人
			
			$logModel= new Merchant_Model_ProductStocketLog();			
			$result2=false;			
			
			if($result){
				//记录日志
				$addLog=array(
					'product_id'=>$productId,
					'user_id'=>$customerId,
					'psl_type'=>1, //库存修改
					"psl_current_value"=>$currentStock,
					"psl_current_value_hold"=>$currentHold,
					'psl_note'=>"Order submission, inventory modify ".$remark,
				);
				$result2=$logModel->add($addLog);
			}
			if($result && $result2){
				return true;				
			}
		}
		
		return false;
	}
	
}